package com.wy2cloud.core.tools;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * 类名: DatetimeTools <br/> 
 * 功能: 日期帮助�? <br/> 
 * 创建日期: 2015-10-29 下午2:06:06 <br/> 
 *
 * @author WYY
 * @version V1.0
 * @since Jdk 1.6
 * @see       
 *
 */
public class DatetimeTools {
	private static String CurrentTime;
	private static String CurrentDate;
	
	/**
	 * 默认日期格式
	 */
	private final static String DefaultFormat="yyyy-MM-dd HH:mm:ss";
	
	/**
	 * 返回本机当前时间
	 * @return
	 */
	public static Date Now(){
		Date NowDate = new Date();
		return NowDate;
	}

	/***
	 * 返回时间指定格式的字符串
	 * @param date
	 * @param format
	 * @return
	 */
	public static String getDatetime(Date date,String format){
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		return formatter.format(date);
	}

	/**
	 * 返回指定格式的时间字符串
	 * @param format
	 * @return
	 */
	public static String getCurrentDatetime(String format){
		return getDatetime(Now(),format);
	}
	/***
	 * 返回当前时间的字符串，格式是：yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static String getCurrentDatetime(){
		return getCurrentDatetime(DefaultFormat);
	}


	/***
	 * 返回年份
	 * @param date
	 * @return
	 */
	public static String getYear(Date date) {
		return getDatetime(date, "yyyy");
	}
	public static Integer getIntYear(Date date) {
		return Integer.parseInt(getYear(date));
	}


	/**
	 * 得到当前的年�?
	 * 返回格式:yyyy
	 * @return String
	 */
	public static String getCurrentYear() {
		return getYear(Now());
	}

	public static Integer getCurrentIntYear(){
		return Integer.parseInt(getCurrentYear());
	}

	/**
	 * 字符串格式化为date
	 * @param date
	 * @param format
	 * @return
	 */
	public static Date Parse(String date, String format){
		try {
			Date d=new SimpleDateFormat(format).parse(date);
			return d;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 字符串格式化为date ,formart:DefaultFormat
	 * @param date
	 * @return
	 */
	public static Date Parse(String date){
		return Parse(date,DefaultFormat);
	}


	/**
	 * 得到当前的月�?
	 * 返回格式:MM
	 * @return String
	 */
	public static String getCurrentMonth() {
		Date NowDate = new Date();

		SimpleDateFormat formatter = new SimpleDateFormat("MM");
		return formatter.format(NowDate);
	}
	/**
	 * 得到当前的日�?
	 * 返回格式:dd
	 * @return String
	 */
	public static String getCurrentDay() {
		Date NowDate = new Date();

		SimpleDateFormat formatter = new SimpleDateFormat("dd");
		return formatter.format(NowDate);
	}
	/**
	 * 得到当前的时间，精确到毫�?,�?19�?
	 * 返回格式:yyyy-MM-dd HH:mm:ss
	 * @return String
	 */
	public static String getCurrentTime() {
		Date NowDate = new Date();
		SimpleDateFormat formatter =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		CurrentTime = formatter.format(NowDate);
		return CurrentTime;
	}

	public static String getDiffTime(int sec){
		Date NowDate = new Date();
		NowDate.setTime(NowDate.getTime()+sec*1000);
		SimpleDateFormat formatter =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String DiffTime = formatter.format(NowDate);
		return DiffTime;

	}

	/**
	 * 得到当前的日�?,�?10�?
	 * 返回格式：yyyy-MM-dd
	 * @return String
	 */
	public static String getCurrentDate() {
		Date NowDate = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		CurrentDate = formatter.format(NowDate);
		return CurrentDate;
	}

	public static String getWeekDay(String currentdate){
		Calendar calendar=Calendar.getInstance();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		int wday;
		try {
			Date date = formatter.parse(currentdate);
			calendar.setTime(date);
			wday = calendar.get(Calendar.DAY_OF_WEEK)-1;
			return new Integer(wday).toString();//addDay(currentdate,weekDay-wday);
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}
	/**
	 * 得到当前日期加上某一个整数，整数代表秒数
	 * 输入参数：currentdate : String 格式yyyy-MM-dd HH:mm:ss
	 * 			addSecond		:  int
	 * 返回格式：yyyy-MM-dd HH:mm:ss
	 */
	public static String addSecond(String currentdate,int add_second){
		GregorianCalendar gc=null;
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		int year,month,day,hours,minute,second;
		try {
			year = Integer.parseInt(currentdate.substring(0,4));
			month = Integer.parseInt(currentdate.substring(5,7))-1;
			day = Integer.parseInt(currentdate.substring(8,10));
			hours = Integer.parseInt(currentdate.substring(11,13));
			minute = Integer.parseInt(currentdate.substring(14,16));
			second = Integer.parseInt(currentdate.substring(17,19));
			gc=new GregorianCalendar(year,month,day,hours,minute,second);
			gc.add(GregorianCalendar.SECOND,add_second);
			return formatter.format(gc.getTime());
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	/**
	 * 得到当前日期加上某一个整数的日期，整数代表天�?
	 * 输入参数：currentdate : String 格式 yyyy-MM-dd
	 * 			add_day		:  int
	 * 返回格式：yyyy-MM-dd
	 */
	public static String addDay(String currentdate,int add_day){
		GregorianCalendar gc=null;
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		int year,month,day;

		try {
			year=Integer.parseInt(currentdate.substring(0,4));
			month=Integer.parseInt(currentdate.substring(5,7))-1;
			day=Integer.parseInt(currentdate.substring(8,10));

			gc=new GregorianCalendar(year,month,day);
			gc.add(GregorianCalendar.DATE,add_day);

			return formatter.format(gc.getTime());
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	public static String addMonth(String currentdate,int add_month){
		GregorianCalendar gc=null;
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		int year,month,day;

		try {
			year=Integer.parseInt(currentdate.substring(0,4));
			month=Integer.parseInt(currentdate.substring(5,7))-1;
			day=Integer.parseInt(currentdate.substring(8,10));

			gc=new GregorianCalendar(year,month,day);
			gc.add(GregorianCalendar.MONTH, add_month);

			return formatter.format(gc.getTime());
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/***
	 * 根据字符串，返回时间对象
	 * @param sDate：String ,格式化为时间的字符串
	 * @return
	 */
	public static Date parse(String sDate) {
        SimpleDateFormat bartDateFormat =new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = bartDateFormat.parse(sDate);
            return date;
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

	public static String getWeekDate(String currentdate,int weekDay){
		Calendar calendar=Calendar.getInstance();
		int wday;

		try {

			//wday = gc.DATE;
			wday = calendar.get(Calendar.DAY_OF_WEEK)-1;

			if(wday==0)
				wday = 7;
			return addDay(currentdate,weekDay-wday);
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}

	public static Date convertToDate(String date) {
		SimpleDateFormat formatter =new SimpleDateFormat("yyyy-MM-dd");
		try{
			return formatter.parse(date);
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	/**
	 *
	 * convertToDateTime:把字符串时间转换为日期时�?. <br/>
	 *
	 * @author gss
	 * @param DateTime
	 * @return
	 */
	public static Date convertToDateTime(String DateTime) {
		SimpleDateFormat formatter =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try{
			return formatter.parse(DateTime);
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	/**
	 * 给定字符串和格式，返回加�?个月的串
	 * 输入�?"1998-11-10"�?"yyyy-MM-dd"
	 * 输出�?"1998-12-10"
	 * @param d
	 * @param format
	 * @return
	 * @author yuzx
	 */
	public static String getStrByaddMonth(String d,String format){
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		try {
			Date date = sdf.parse(d);
			GregorianCalendar gc = new GregorianCalendar();
			gc.setTime(date);
			gc.add(Calendar.MONTH, 1);
			return sdf.format(gc.getTime());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 给定字符串和格式，返回加�?个星期的�?
	 * 输入�?"1998-11-10"�?"yyyy-MM-dd"
	 * 输出�?"1998-12-10"
	 * @param d
	 * @param format
	 * @return
	 * @author yuzx
	 */
	public static String getStrByaddWeek(String d,String format){
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		try {
			Date date = sdf.parse(d);
			GregorianCalendar gc = new GregorianCalendar();
			gc.setTime(date);
			gc.add(Calendar.WEEK_OF_YEAR, 1);
			return sdf.format(gc.getTime());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	//"yy-MM-dd"
	public static int compareToDate(String src,String obj,String format) throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		GregorianCalendar srcStartGc = new GregorianCalendar();
		GregorianCalendar srcEndGc = new GregorianCalendar();
		srcStartGc.setTime(sdf.parse(src));
		srcEndGc.setTime(sdf.parse(obj));
		return srcStartGc.compareTo(srcEndGc);
	}

	/**
	 * 方法说明:判断传入的日�?,如果是当天则返回 yyyy-MM-dd hh:mm:DD 如果不是当天则返�? yyyy-MM-dd
	 * @param dataTime �?要计算的时间
	 * @return
	 */
	public static String computationDateTime(String dataTime)
	{
    	String result = "";
    	if(StringUtils.isNotBlank(dataTime)){
    		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    		try {
				Date dd = sdf.parse( dataTime );
				if(DateUtils.isSameDay( dd , new Date())) {
					return new SimpleDateFormat("HH:mm").format( dd );
				} else {
					return new SimpleDateFormat("yyyy-MM-dd").format( dd );
				}
			} catch (ParseException e) {
				e.printStackTrace();
			}

		}
    	return result;
	}

	/**
	 * 计算2个日期相差的月数
	 * @param s
	 * @param e
	 * @return
	 * zby
	 */
	public static int getMonth(String data1,String data2){
		int result=0;
		try{
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Date startdate = sdf.parse(data1);
			Date enddate = sdf.parse(data2);

			Calendar start = Calendar.getInstance();
			start.setTime(startdate);
			Calendar end = Calendar.getInstance();
			end.setTime(enddate);
			Calendar temp=Calendar.getInstance();
			temp.setTime(enddate);
			temp.add(Calendar.DATE,1);

			int y=end.get(Calendar.YEAR)-start.get(Calendar.YEAR);
			int m=end.get(Calendar.MONTH)-start.get(Calendar.MONTH);

			result= y*12+m+1;
			System.err.println("sssss="+start.get(Calendar.DATE));//
//			if((start.get(Calendar.DATE)==1) && (temp.get(Calendar.DATE)==1)){//前后都不破月
//				result= y*12+m+1;
//			}
//			else if((start.get(Calendar.DATE)!=1) && (temp.get(Calendar.DATE)==1)){//前破月后不破�?
//				result= y*12+m;
//			}
//			else if((start.get(Calendar.DATE)==1) && (temp.get(Calendar.DATE)!=1)){//前不破月后破�?
//				result= y*12+m;
//			}
//			else {//前破月后破月
//		     result= (y*12+m-1)<0?0:(y*12+m-1);
//			}

			SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM");
			Date date = sdf.parse(data1);

			System.err.println("st0="+sdf1.format(date));
			System.err.println("st="+DatetimeTools.getStrByaddMonth(data1,"yyyy-MM"));
			String [] res = new String[result];
			res[0] = sdf1.format(date);
			int i=1;
			while(i<result){
				res[i] = DatetimeTools.getStrByaddMonth(res[i-1],"yyyy-MM");
				i++;
			}
			for(int j=0;j<res.length;j++){//具体�?
				System.err.println("str["+j+"]="+res[j]);//
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		System.err.println("result="+result);
		return result;
	}
	public static int getDay(String data1,String data2){
		int result=0;
		try{
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Date startdate = sdf.parse(data1);
			Date enddate = sdf.parse(data2);

			Calendar start = Calendar.getInstance();
			start.setTime(startdate);
			Calendar end = Calendar.getInstance();
			end.setTime(enddate);

			Calendar temp=Calendar.getInstance();
			temp.setTime(enddate);
			temp.add(Calendar.DATE,1);

			int y=end.get(Calendar.YEAR)-start.get(Calendar.YEAR);
			int m=end.get(Calendar.MONTH)-start.get(Calendar.MONTH);
			int d=end.get(Calendar.DATE)-start.get(Calendar.DATE);

			result= y*12+m+1+d;


		}catch(Exception e){
			e.printStackTrace();
		}
		return result;
	}
	public static int getDays(String data1,String data2){
		int result=0;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try{
			Date startdate = sdf.parse(data1);
			Date enddate = sdf.parse(data2);
			long milliseconds = enddate.getTime() - startdate.getTime();
			long days = milliseconds / (long) (1000 * 60 * 60 * 24);
			result=Integer.parseInt(days+"");
		}catch(Exception e){
			e.printStackTrace();
		}
		return result;
	}
	public static int getWeeks(String data1,String data2){
		int result=0;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try{
			Date startdate = sdf.parse(data1);
			Date enddate = sdf.parse(data2);
			long milliseconds = enddate.getTime() - startdate.getTime();
			long days = milliseconds / (long) (1000 * 60 * 60 * 24 * 7);
			result=Integer.parseInt(days+"");
		}catch(Exception e){
			e.printStackTrace();
		}
		return result;
	}
	public static int getMonths(String data1,String data2){
		int result=0;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		try{
			Date startdate = sdf.parse(data1);
			Date enddate = sdf.parse(data2);
			int manth = enddate.getMonth()-startdate.getMonth();
			if(enddate.getDay()<startdate.getDay()){
				manth--;
			}
			result=Integer.parseInt(manth+"");
		}catch(Exception e){
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 获取时间�?
	 * @param time1 time2 �?要计算的时间
	 * @return
	 */
	public static String getTimeDeffer(String time1,String time2)
	{
    	String result = "";
    	if(StringUtils.isNotBlank(time1)&&StringUtils.isNotBlank(time2)){
    		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    		try {
				long dt1 = sdf.parse( time1 ).getTime();
				long dt2 = sdf.parse( time2 ).getTime();
				long dc = Math.abs(dt2-dt1);

				long seconds = dc/1000;
				long date = seconds/(24*60*60);     //相差的天�?
				long hour = (seconds-date*24*60*60)/(60*60);//相差的小时数
				long minut = (seconds-date*24*60*60-hour*60*60)/(60);//相差的分钟数
				long second = (seconds-date*24*60*60-hour*60*60-minut*60);//相差的秒�?
				return (date==0?"":(date+"�?"))+(hour==0?"":(hour+"小时"))+(minut==0?"":(minut+"�?"))+(second==0?"":(second+"�?"));
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
    	return result;
	}
	/**
	 * 获取时间差，秒数
	 * @param time1 time2 �?要计算的时间
	 * @return
	 */
	public static Long getTimeSecondDeffer(String time1,String time2)
	{
		Long result = 0L;
		if(StringUtils.isNotBlank(time1)&&StringUtils.isNotBlank(time2)){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			try {
				long dt1 = sdf.parse( time1 ).getTime();
				long dt2 = sdf.parse( time2 ).getTime();
				long dc = Math.abs(dt2-dt1);

				long seconds = dc/1000;
				long date = seconds/(24*60*60);     //相差的天�?
				long hour = (seconds-date*24*60*60)/(60*60);//相差的小时数
				long minut = (seconds-date*24*60*60-hour*60*60)/(60);//相差的分钟数
				long second = (seconds-date*24*60*60-hour*60*60-minut*60);//相差的秒�?
				return date*24*60*60+hour*60*60+minut*60+second;
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return result;
	}

	/**
	 * 获取时间差，秒数
	 * @param time1 time2 �?要计算的时间
	 * @return
	 */
	/**
	 *
	 * getTimePureSecondDeffer:获取时间差，秒数不取绝对�?. <br/>
	 * (time2大于time1返回正数，反之返回负�?).<br/>
	 *
	 * @author zhanfc
	 * @param time1
	 * @param time2
	 * @return
	 */
	public static Long getTimePureSecondDeffer(String time1,String time2)
	{
		boolean flag = true;
		Long result = 0L;
		if(StringUtils.isNotBlank(time1)&&StringUtils.isNotBlank(time2)){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			try {
				long dt1 = sdf.parse( time1 ).getTime();
				long dt2 = sdf.parse( time2 ).getTime();
				long dc = dt2-dt1;
				if(dc <0)
					flag = false;
				long absDc = Math.abs(dc);

				long seconds = absDc/1000;
				long date = seconds/(24*60*60);     //相差的天�?
				long hour = (seconds-date*24*60*60)/(60*60);//相差的小时数
				long minut = (seconds-date*24*60*60-hour*60*60)/(60);//相差的分钟数
				long second = (seconds-date*24*60*60-hour*60*60-minut*60);//相差的秒�?
				if(flag)
					return date*24*60*60+hour*60*60+minut*60+second;
				else
					return Long.parseLong("-"+(date*24*60*60+hour*60*60+minut*60+second));
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return result;
	}
	/**
	 * 获取时间�?
	 * @param time1 time2 �?要计算的时间
	 * @return
	 */
	public static String getTimeDefferByDate(String time1,String time2)
	{
    	String result = "";
    	if(StringUtils.isNotBlank(time1)&&StringUtils.isNotBlank(time2)){
    		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    		try {
				long dt1 = sdf.parse( time1 ).getTime();
				long dt2 = sdf.parse( time2 ).getTime();
				long dc = Math.abs(dt2-dt1);

				long seconds = dc/1000;
				long date = seconds/(24*60*60);     //相差的天�?
				long hour = (seconds-date*24*60*60)/(60*60);//相差的小时数
				long minut = (seconds-date*24*60*60-hour*60*60)/(60);//相差的分钟数
//				long second = (seconds-date*24*60*60-hour*60*60-minut*60);//相差的秒�?
				return (date+1)+"";
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
    	return result;
	}

	/**
	 *
	 * UnixTimestamp2StrTime: 将unix的时间戳转换为普通的时间格式
	 * TODO(这里描述这个方法的注意事�? �? 可�??).<br/>
	 *
	 * @author HT
	 * @param currentTimeMillis unix的时间戳
	 * @param dateFormat 时间格式 例如�? "dd/MM/yyyy HH:mm:ss"
	 * @return
	 */
	public static String UnixTimestamp2StrTime(long currentTimeMillis ,String dateFormat){
		if(StringUtils.isBlank(dateFormat))
			dateFormat = "yyyy-MM-dd HH:mm:ss";
		DateFormat formatter = new SimpleDateFormat(dateFormat);
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(currentTimeMillis);
		return formatter.format(calendar.getTime());
	}

	/**
	 * 将普通的时间格式转换为unix的时间戳
	 * @param strTime    字符串时间（格式要和dateFormat的格式相同） 例如�? "09/22/2008 16:33:00"
	 * @param dateFormat 时间格式 例如�? "dd/MM/yyyy HH:mm:ss"
	 * @return unix时间戳（单位：秒�?
	 * */
	public static long strTime2UnixTimestamp(String strTime, String dateFormat){
		long time = 0l;
		try {
			time = new SimpleDateFormat (dateFormat).parse(strTime).getTime();
		} catch (ParseException e) {
			e.printStackTrace();
			return 0l;
		}
		return time/1000;
	}
	
	public static String  getTime(){
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
		return sdf.format(new Date());
	}
	
	public static String  getTimeHm(){
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
		return sdf.format(new Date());
	}
	/**
	 * 当day为负数时，得到前day天的日期字符�?,
	 * 若为正数时，则得到后day天的日期字符�?
	 * @param sdate
	 * @param day
	 * @return
	 */
	public static String getPreDay(int day){
		Calendar calendar = Calendar.getInstance();//此时打印它获取的是系统当前时�?
        calendar.add(Calendar.DATE, day);    //得到前一�?
        String  yestedayDate  = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
        return yestedayDate;
	}
	/**
	 * 两个日期比较，是否相差了min分钟，如果是在min分钟后，返回true,否则，返回false.
	 * @param oldDate
	 * @param date
	 * @param min
	 * @return
	 */
	public static boolean compare(Date oldDate,Date date,int min){
		long sec = min*60*1000L;//
		long old = oldDate.getTime();
		long nwe = date.getTime();
		if((old+sec)>nwe) return false;
		return true;
	}
	
	public static String getDTFormat(String sdate){
		String format = "";
		try {
			Date current = new Date();
			long microCurrent = current.getTime();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date date = sdf.parse(sdate);
			long microDate = date.getTime();
			long secPre = 60*60*1000L;//X分钟�?
			long hourPre = 6*60*60*1000L;//6小时�?
			String currentYear = new SimpleDateFormat("yyyy").format(current);//取得当前�?
			String juageYear = new SimpleDateFormat("yyyy").format(date);
			String currentDay = getPreDay(0);//取得当前�?
			String yesterdayPre = getPreDay(-1);//获取昨天
			String beforeYesterdayPre = getPreDay(-2);//获取前天
			long difference = microCurrent - microDate;
			if(difference<secPre){
				long sec = difference/(60*1000);
				return sec==0L?"刚刚":sec+"分钟�?";
			}
			else if(difference<hourPre){
				long sec = difference/(60*60*1000);
				return sec == 0L?"1":sec+"小时�?";
			}
			else if(sdate.substring(0,10).equals(currentDay)){
				return new SimpleDateFormat("HH:mm").format(date);
			}
			else if(sdate.substring(0, 10).equals(yesterdayPre)){
				return "昨天 "+new SimpleDateFormat("HH:mm").format(date);
			}
			else if(sdate.substring(0, 10).equals(beforeYesterdayPre)){
				return "前天"+new SimpleDateFormat("HH:mm").format(date);
			}
			else if(currentYear.equals(juageYear)){
				return new SimpleDateFormat("M月dd�? HH:mm").format(date);
			}
			else{
				return new SimpleDateFormat("yyyy-M-dd").format(date);
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return format;
	}
	
	/**
	 * 
	 * getWeekOfDate:(获取两个日期之间相差的星期数). <br/> 
	 * (这里描述这个方法的注意事�? �? 可�??).<br/> 
	 * 
	 * @author Administrator 
	 * @param str1
	 * @param str2
	 * BJ
	 * @return
	 */
	public static int getWeekOfDate(String str1,String str2){
		int res  = 0;
		try{
			//获取该天是本星期的第几天
			String sdWeek = getWeekDay(str1);
			String edWeek =  getWeekDay(str2);
			//获取该星期的�?后一�?
			String sdLast = addDay(str1,7-(Integer.valueOf(sdWeek)));
			String edLast = addDay(str2,7-(Integer.valueOf(edWeek)));
			//获取两日期相差的天数
			int dif = getDays(sdLast,edLast);
			res  = dif/7;
			
		}catch(Exception e){
			e.printStackTrace();
		}
		return res;
	}
	
	/**
	 * @description 获取前N周的起始日期 日期格式 yyyy-MM-dd
	 * @param lastNum 前N�? ,0为当前周,1为上�?,2为上2�?,依次类推
	 * @return yyyy-MM-dd
	 * 不为周日
	 * 郭磊
	 */
	public static String getLastWeekStartDateByNum(int lastNum) {
		int num = 7 * (lastNum - 1);
		GregorianCalendar calendar = new GregorianCalendar();
		int minus = calendar.get(GregorianCalendar.DAY_OF_WEEK) + 1;
		calendar.add(GregorianCalendar.DATE, -minus - num);
		calendar.add(GregorianCalendar.DATE, -4);
		String begin = new java.sql.Date(calendar.getTime().getTime()).toString();
		return begin;
	}
	
	/**
	 * @description 获取前N周的起始日期 日期格式 yyyy-MM-dd
	 * @param lastNum 前N�? ,0为当前周,1为上�?,2为上2�?,依次类推
	 * @return yyyy-MM-dd
	 * 如果周日
	 * 郭磊
	 */
	public static String getLastWeekStartDateSixByNum(int lastNum){
		int num = 7 * (lastNum - 1);
		GregorianCalendar calendar = new GregorianCalendar();
		int minus = calendar.get(GregorianCalendar.DAY_OF_WEEK) + 1;
		calendar.add(GregorianCalendar.DATE, -minus - num);
		calendar.add(GregorianCalendar.DATE, -11);
		String begin = new java.sql.Date(calendar.getTime().getTime()).toString();
		return begin;
	}
	
	/**
	 * 
	 * getDateDays:获取指定日期的天�?. <br/> 
	 * (这里描述这个方法的注意事�? �? 可�??).<br/> 
	 * 
	 * @author zhanfc 
	 * @param date
	 * @return
	 */
	public static Long getDateDays(String date){
		Long result = 0L;
		if(StringUtils.isNotBlank(date)){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			try {
				long dt = sdf.parse( date ).getTime();
				long dc = Math.abs(dt);
				long seconds = dc/1000;   
				long days = seconds/(24*60*60);     //天数   
				return days;
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return result;
	}
	
	public static void main(String []args){
		try {
//			System.err.println(getTimeDefferByDate("08:00","07:00"));
			//String strTime = new SimpleDateFormat("yyyy-MM-dd HH").format(new Date());
			//long timeStamp = strTime2UnixTimestamp("2013-11-19 00", "yyyy-MM-dd HH");
//			String sdate = "2014-03-31 08:55";
//			System.out.println(TimeHelper.getTimeSecondDeffer("2014-09-19 11:12:11","2014-09-19 13:11:11"));;
			//getWeeks("2014-01-11 11:11:11","2014-11-18 18:12:12");
			//System.out.println(timeStamp);
			System.out.println(DatetimeTools.getTimePureSecondDeffer("2014-09-19 11:12:13", "2014-09-19 11:12:14"));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
